package com.hoshiicloud.goods.controller;

import com.google.gson.Gson;
import com.hoshiicloud.common.rpc.Response;
import io.swagger.annotations.ApiOperation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

/**
 * @author MarkerCCC
 * @date 2019-09-20 - 10:16
 */
@Slf4j
@RestController
@RequestMapping("/moduleController")
public class ModuleController {

    @GetMapping("/getRequestUrl")
    public Response getRequestUrl(HttpServletRequest httpServletRequest) {
        WebApplicationContext wc = WebApplicationContextUtils
                .getRequiredWebApplicationContext(httpServletRequest.getSession().getServletContext());
        RequestMappingHandlerMapping rmhp = wc
                .getBean(RequestMappingHandlerMapping.class);
        Map<RequestMappingInfo, HandlerMethod> map = rmhp.getHandlerMethods();
        Map<String, List<String>> urlMessageLinkMultiValueMap = new HashMap<>();
        HashSet<Class<?>> classes = new HashSet<>();
        map.forEach((requestMappingInfo, handlerMethod) -> {
            // 拿到一个类中的所有url
            Class<?> beanType = handlerMethod.getBeanType();
            classes.add(beanType);
        });

        for (Class<?> beanType : classes) {
            Method[] methods = beanType.getMethods();
            String simpleName = beanType.getSimpleName();
            RequestMapping requestAnnotation = beanType.getAnnotation(RequestMapping.class);
            String headUrl = "";
            if (requestAnnotation != null) {
                String[] valueArr = requestAnnotation.value();
                headUrl = valueArr[0];
            }

            for (Method method : methods) {
                ApiOperation apiOperationAnnotation = method.getAnnotation(ApiOperation.class);
                GetMapping getAnnotation = method.getAnnotation(GetMapping.class);
                PostMapping postAnnotation = method.getAnnotation(PostMapping.class);
                PutMapping putAnnotation = method.getAnnotation(PutMapping.class);
                DeleteMapping deleteAnnotation = method.getAnnotation(DeleteMapping.class);
                RequestMapping requestMethodAnnotation = method.getAnnotation(RequestMapping.class);
                if (getAnnotation != null) {
                    this.addLinkMultiValueMap(urlMessageLinkMultiValueMap, simpleName, headUrl, apiOperationAnnotation,
                            getAnnotation.value());
                }
                if (postAnnotation != null) {
                    this.addLinkMultiValueMap(urlMessageLinkMultiValueMap, simpleName, headUrl, apiOperationAnnotation,
                            postAnnotation.value());
                }
                if (putAnnotation != null) {
                    this.addLinkMultiValueMap(urlMessageLinkMultiValueMap, simpleName, headUrl, apiOperationAnnotation,
                            putAnnotation.value());
                }
                if (deleteAnnotation != null) {
                    this.addLinkMultiValueMap(urlMessageLinkMultiValueMap, simpleName, headUrl, apiOperationAnnotation,
                            deleteAnnotation.value());
                }
                if (requestMethodAnnotation != null) {
                    this.addLinkMultiValueMap(urlMessageLinkMultiValueMap, simpleName, headUrl, apiOperationAnnotation,
                            requestMethodAnnotation.value());
                }
            }
        }
        log.error("{}", new Gson().toJson(urlMessageLinkMultiValueMap));
        return Response.data(urlMessageLinkMultiValueMap);
        // return urlMapping.getData();
    }

    private void addLinkMultiValueMap(Map<String,List<String>> urlMessageLinkMultiValueMap, String simpleName,
            String headUrl,
            ApiOperation annotation, String[] valueArr) {
        List<String> valueList=urlMessageLinkMultiValueMap.get(simpleName);
        if(valueList==null){
            valueList=new ArrayList<>();
        }
        for (String value : valueArr) {
            if (annotation != null) {
                String annotationValue = annotation.value();
                valueList.add(headUrl + value + ":" + annotationValue);
            } else {
                valueList.add(headUrl + value);
            }
        }
        urlMessageLinkMultiValueMap.put(simpleName,valueList);
    }
}
